<script lang="ts" setup>
import { ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { GetCollectionData, PatchModifyType, PostCreateData } from '@/api/modules/collection'
import PageNumber from '@/store/modules/PageNumberCache'

const imgDomain = 'https://img1.eprhan.cc'
const router = useRouter()
const PageNumberCache = PageNumber()

// 查询表单
const findFrom = reactive({
  ActStatus: '~',
})

const dataList: any = reactive({
  list: [],
})

const loading = ref(true)
const total = ref(0)
let PageSize = 20

async function GetDataList(PageIndex: number = 1, PageSize: number = 20) {
  const params = {
    PageIndex,
    PageSize,
    ...(findFrom.ActStatus !== '~' && { ActStatus: findFrom.ActStatus }),
  }
  await GetCollectionData(params).then(async (dataRes: any) => {
    if (dataRes.succeeded) {
      dataList.list = dataRes.data.items
      total.value = dataRes.data.total
      loading.value = false
    }
    else {
      ElMessage.error(`查询失败。${dataRes.errors}`)
      loading.value = false
    }
  })
    .catch(() => {
      loading.value = false
    })
  loading.value = false
}

// 查询
function Inquire() {
  GetDataList(1, PageSize)
}

// 修改状态
function ModifyType(params: any) {
  PatchModifyType(params).then((res: any) => {
    if (res.statusCode === 200) {
      ElMessage.success('修改成功')
      // GetDataList(PageNum, PageSize)
    }
    else {
      ElMessage.error(`修改失败${res.errors}`)
    }
  })
}
// 添加
function AddData() {
  router.push({
    name: 'collectionDetail',
  })
}
// 编辑
function EditData(id: number) {
  router.push({
    name: 'collectionDetail',
    query: {
      id,
    },
  })
}
// 关联配置
function categoryItem(id: number) {
  router.push({
    name: 'categoryDetail',
    query: {
      id,
    },
  })
}

// 复制
function CopyData(row: any) {
  ElMessageBox.confirm(
    '你确定要复制该条吗？',
    '提示',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    },
  )
    .then(() => {
      const params = {
        bannerPic: row.bannerPic,
        bottomPic: row.bottomPic,
        sharePic: row.sharePic,
        shareTxt: row.shareTxt,
        getBtnPic: row.getBtnPic,
        usedBtnPic: row.usedBtnPic,
        actStatus: row.actStatus,
      }
      // console.log(params)

      PostCreateData(params).then((res: any) => {
        if (res.statusCode === 200) {
          ElMessage.success('复制成功')
          GetDataList(PageNumberCache.GetCollectionPage())
        }
        else {
          ElMessage.error(`复制失败${res.errors}`)
        }
      })
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '取消复制',
      })
    })
}

// 翻页
function handleSizeChange(val: number) {
  GetDataList(1, val)
  PageSize = val
}
function handleCurrentChange(val: number) {
  GetDataList(val, PageSize)
  PageNumberCache.SetCollectionPage(val)
}

const nowPageNum = computed(() => {
  return PageNumberCache.GetCollectionPage()
})

onMounted(async () => {
  GetDataList(PageNumberCache.GetCollectionPage())
})
</script>

<template>
  <div>
    <PageMain>
      <SearchBar :show-toggle="false">
        <div class="header">
          <el-form label-positio="right" label-width="auto">
            <div class="from">
              <el-form-item label="活动状态">
                <el-select v-model="findFrom.ActStatus" placeholder="请选择状态">
                  <el-option label="全部" value="~" />
                  <el-option label="启用" :value="1" />
                  <el-option label="停用" :value="-1" />
                </el-select>
              </el-form-item>
            </div>
          </el-form>
          <div class="text-right">
            <el-button @click="Inquire">
              查询
            </el-button>
          </div>
        </div>
      </SearchBar>
      <ElDivider border-style="dashed" />
      <div class="mb-5 text-right">
        <el-button type="success" @click="AddData">
          添加
        </el-button>
      </div>
      <el-table v-loading="loading" :data="dataList.list" stripe style="width: 100%;" border>
        <el-table-column align="center" prop="collectionId" label="配置ID" width="100" />
        <el-table-column align="center" prop="bannerPic" label="Banner图" width="100">
          <template #default="scope">
            <el-avatar shape="square" :size="60" :src="imgDomain + scope.row.bannerPic" />
          </template>
        </el-table-column>
        <el-table-column align="center" prop="bottomPic" label="底部图片" width="100">
          <template #default="scope">
            <el-avatar shape="square" :size="60" :src="imgDomain + scope.row.bottomPic" />
          </template>
        </el-table-column>
        <el-table-column align="center" prop="getBtnPic" label="获取按钮图片" width="120">
          <template #default="scope">
            <el-avatar shape="square" :size="60" :src="imgDomain + scope.row.getBtnPic" />
          </template>
        </el-table-column>
        <el-table-column align="center" prop="usedBtnPic" label="已使用按钮图片" width="140">
          <template #default="scope">
            <el-avatar shape="square" :size="60" :src="imgDomain + scope.row.usedBtnPic" />
          </template>
        </el-table-column>
        <el-table-column align="center" prop="sharePic" label="分享图" width="100">
          <template #default="scope">
            <el-avatar shape="square" :size="60" :src="imgDomain + scope.row.sharePic" />
          </template>
        </el-table-column>
        <el-table-column header-align="center" width="300" prop="shareTxt" label="分享语" />
        <el-table-column align="center" width="240" prop="addTime" label="添加时间" />
        <el-table-column align="center" width="240" prop="updateTime" label="更新时间" />
        <el-table-column align="center" label="状态" fixed="right" width="180">
          <template #default="scope">
            <el-switch
              v-model="scope.row.actStatus"
              active-text="启用"
              inactive-text="未启用"
              :active-value="1"
              :inactive-value="-1"
              @click="ModifyType({ id: scope.row.collectionId, actStatus: scope.row.actStatus })"
            />
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作" fixed="right" min-width="280">
          <template #default="scope">
            <el-button type="success" @click="categoryItem(scope.row.collectionId)">
              关联配置
            </el-button>
            <el-button type="success" @click="CopyData(scope.row)">
              复制
            </el-button>
            <el-button type="primary" @click="EditData(scope.row.collectionId)">
              编辑
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div style="height: 10px;" />
      <el-pagination
        :default-page-size="20"
        :page-sizes="[5, 10, 20, 50, 100]"
        layout="total, sizes, ->, prev, pager, next, jumper"
        :total="total"
        :default-current-page="nowPageNum"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </PageMain>
  </div>
</template>

<style scoped>
.header {
  display: grid;
  grid-template-columns: 6fr 1fr;

  .from {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
    grid-gap: 20px;

    .el-form-item {
      margin-bottom: 0;
    }
  }
}

.scrollbar-flex-content {
  display: flex;
  max-width: 300px;

  .scrollbar-flex-img {
    display: flex;
    flex-shrink: 0;
    align-items: center;
    justify-content: center;
    width: auto;
    height: 60px;
    margin-right: 10px;
  }
}
</style>
